// book list modal ctrl
var app = angular.module('app');
app.controller('bookListModalCtrl',
    ['$scope', '$modalInstance', '$window', 'bookService', 'officialWeb',
        function ($scope, $modalInstance, $window, bookService, officialWeb) {
            $scope.query = {
                isPassed: true // book must be passed
            };
            $scope.currentPage = 1;
            $scope.selectedInformation = [];
            // search
            $scope.search = function (query, page) {
                bookService.list(query, page).then(function (rsp) {
                    $scope.rsp = rsp;
                    $scope.currentPage = rsp.currentPage;
                    $scope.totalCount = rsp.totalCount;
                    $scope.pageCount = rsp.pageCount;
                    $scope.perPageCount = rsp.perPageCount;
                }, function (err) {
                    $modalInstance.dismiss(err);
                });
            };
            // config
            $scope.config = function () {
                $modalInstance.close($scope.selectedInformation);
            };
            // close
            $scope.close = function () {
                $modalInstance.dismiss();
            };
            // go to book detail
            $scope.bookDetail = function (index) {
                var url = officialWeb.addr + '/' + $scope.rsp.data[index]._id;
                $window.open(url, $scope.rsp.data[index].name);
            };
            $scope.pageChanged = function () {
                $scope.search($scope.query, $scope.currentPage);
            };
            $scope.clickSearch = function () {
                $scope.search($scope.query, 1);
            };
            $scope.search(null, $scope.currentPage);
        }]);
